<?php

include_once 'common.php';

$subjects = json_stringify($payload->subjects);
$traineeLicences = json_stringify($payload->traineeLicences);
$subjectsComments = json_stringify($payload->subjectsComments);
$failedSubjects = json_stringify($payload->failedSubjects);
$evaluations = json_stringify($payload->evaluations);
$history = json_stringify($payload->history);
$updateTime = date('Y-m-d H:i:s');

$sql = "DELETE FROM `trex_exercises` WHERE `title`='{$payload->title}' AND `year`={$payload->year} AND `cycle`={$payload->cycle} AND `office`='{$payload->office}'";
exec_sql($sql);
$sql = "INSERT INTO `trex_exercises`(`number`, `title`, `year`, `cycle`, `type`, `office`, `subjects`, `hours`, `description`, `startDate`, `endDate`, `author`, `updateTime`, `valid`, `disposed`) VALUES (null, '{$payload->title}', {$payload->year}, {$payload->cycle}, {$payload->type}, '{$payload->office}', '{$subjects}', {$payload->hours}, '{$payload->description}', '{$payload->startDate}', '{$payload->endDate}', '{$payload->author}', '{$updateTime}', {$payload->valid}, {$payload->disposed})";
$result = exec_sql($sql);
output(['sql' => $sql, 'result' => $result, 'payload' => $payload]);